package com.ccx.coachsite.ui.manage;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.ccx.coachsite.R;
import com.ccx.coachsite.bus.CommonBus;
import com.ccx.coachsite.bus.CustomInfo;
import com.ccx.coachsite.bus.SharedKey;
import com.ccx.coachsite.ui.own.ExpandAdapterBase;
import com.ccx.coachsite.ui.own.OwerBaseView;
import com.ccx.coachsite.util.SharedPreferencesUtil;
import com.ccx.coachsite.util.StringUtil;
import com.ccx.coachsite.webimage.ImageLoader;

public class ManageCustomGroupAdapter extends ExpandAdapterBase<CustomInfo>{

	public enum CustomType
	{
		none,
		CustomInvite,
		CustomUninvite,
		CustomMessage,
	}
	private OwerBaseView superView;
	public CustomType mCustomType;
	
	public ManageCustomGroupAdapter(Context context, List<CustomInfo> list1,
			List<CustomInfo> list2, OwerBaseView superView, Integer viewType) {
		super(context, list1, list2);
		mCustomType = CustomType.values()[viewType];
		this.superView = superView;
	}
	
	@Override
	public int getGroupCount() {
		return 1;
	}
	
	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		if(mCustomType == CustomType.CustomUninvite)
		{
			return mList2.size();
		}
		else
		{
			return mList1.size();
		}
	}
	
	public void doInvate(String mobiles)
	{
		CommonBus bus = new CommonBus(mContext);
		String userId = SharedPreferencesUtil.getValue(mContext,
				SharedKey.Shared_UserId_Key, "");
		bus.setInviteParam(userId, mobiles);
		bus.doTask();
	}
	
	class OnIntiveListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			Object obj = v.getTag();
			if(obj != null)
			{
				String mobile = v.getTag().toString();
				doInvate(mobile);
			}
		}
	}
	
	class OnAllIntiveListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			String message = "";
			for(int i = mList2.size() - 1; i>= 0; i--)
			{
				if(!StringUtil.isEmpty(mList2.get(i).getTianYi()))
					message += mList2.get(i).getTianYi()+",";
			}
			if(message.length() > 0)
				message = message.substring(0, message.length() - 1);
			
			if(!StringUtil.isEmpty(message))
			{
				doInvate(message);
			}
		}
	}
	
	class OnMessageListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			Object obj = v.getTag();
			if(obj != null)
			{
				CustomInfo cinfo = (CustomInfo)v.getTag();
				cinfo.setMessageBtn((Button)v);
				superView.SwitchView(ManageKey.ll_manage_custom_message, cinfo);
			}
		}
	}
	
	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
			View convertView, ViewGroup parent) {
		ViewHolder holder;
		
		
		convertView = mInflater.inflate(R.layout.manage_custom_invited_list_item, parent, false);
		holder = new ViewHolder();
		holder.nName = (TextView) convertView.findViewById(R.id.manage_custom_invite_title);
		holder.nPhone = (TextView) convertView.findViewById(R.id.manage_custom_invite_phone);
		holder.nBaseLine = (TextView) convertView.findViewById(R.id.manage_custom_invite_baseline);
		holder.nMode = (TextView) convertView.findViewById(R.id.manage_custom_invite_mode);
		holder.nExBaseLine = (TextView) convertView.findViewById(R.id.manage_custom_info_ex_baseline);
		holder.nExMode = (TextView) convertView.findViewById(R.id.manage_custom_info_ex_mode);
		holder.mIcon = (ImageView) convertView.findViewById(R.id.manage_custom_invite_image);
		holder.nButton = (Button) convertView.findViewById(R.id.manage_custom_invite_buttom);
		holder.nMessageTime = (TextView) convertView.findViewById(R.id.text_custom_message_time);
		holder.nMessageCount = (TextView) convertView.findViewById(R.id.text_custom_message_count);
		
		View messagePanel =  convertView.findViewById(R.id.manage_custom_manage_panel);
		View ExInfoPanel =  convertView.findViewById(R.id.manage_custom_info_ex_panel);
		ImageView flagImg = (ImageView)convertView.findViewById(R.id.manage_custom_image_flag);
		if(mCustomType == CustomType.CustomInvite)
		{
			messagePanel.setVisibility(View.GONE);
			flagImg.setVisibility(View.VISIBLE);
			holder.nButton.setVisibility(View.GONE);
			ExInfoPanel.setVisibility(View.VISIBLE);
			holder.nBaseLine.setVisibility(View.GONE);
			holder.nMode.setVisibility(View.GONE);
			holder.nName.setVisibility(View.GONE);
		}
		else if(mCustomType == CustomType.CustomUninvite)
		{
			messagePanel.setVisibility(View.GONE);
			flagImg.setVisibility(View.GONE);
			holder.nButton.setVisibility(View.VISIBLE);
			ExInfoPanel.setVisibility(View.GONE);
		}
		else if(mCustomType == CustomType.CustomMessage)
		{
			messagePanel.setVisibility(View.VISIBLE);
			flagImg.setVisibility(View.GONE);
			holder.nButton.setVisibility(View.GONE);
			holder.nMode.setVisibility(View.GONE);
			holder.nBaseLine.setVisibility(View.GONE);
			ExInfoPanel.setVisibility(View.GONE);
		}
		
		int id = -1;
		CustomInfo cinfo = null;
		if(mCustomType == CustomType.CustomUninvite)
		{
			cinfo = mList2.get(childPosition);
			id = cinfo.getId();
		}
		else
		{
			cinfo = mList1.get(childPosition);
			id = cinfo.getId();
		}

		if(id == -1)
		{
			holder.mIcon.setVisibility(View.INVISIBLE);
			holder.nName.setText("邀请全部客户");
			holder.nPhone.setText("邀请全部客户");
			holder.nMode.setText("一键快捷邀请客户加入");
			holder.nBaseLine.setVisibility(View.GONE);
			holder.nButton.setText("全部邀请");
			holder.nButton.setTextColor(mContext.getResources().getColor(R.color.blue_title_color));
			holder.nButton.setOnClickListener(new OnAllIntiveListener());
		}
		else
		{
			String str = cinfo.getCustomNickName();
			if(str != null && !str.equals("null"))
			{
				holder.nName.setText(str);
				if(mCustomType != CustomType.CustomInvite)
					holder.nName.setVisibility(View.VISIBLE);
			}
			else
			{
				holder.nName.setVisibility(View.GONE);
			}
			
			holder.nPhone.setText(cinfo.getTianYi());
			holder.nBaseLine.setText("基准值:"+cinfo.getBaseLine());
			holder.nMode.setText("操作系统:"+cinfo.getOs());
			holder.nExBaseLine.setText("基准值:"+cinfo.getBaseLine());
			holder.nExMode.setText("操作系统:"+cinfo.getOs());
			
			if(mCustomType == CustomType.CustomMessage)
			{
				holder.nMessageCount.setText(cinfo.getMessageCount()+"");
				if(cinfo.getMessageCount() <= 0)
					holder.nMessageCount.setVisibility(View.INVISIBLE);
				
				holder.nMessageTime.setText(cinfo.getLastMessageTime());
			}
			String picturePath = cinfo.getIcon();
			if (!StringUtil.isEmpty(picturePath)) 
			{
				holder.mIcon.setImageResource(R.drawable.ower_image_none);
				ImageLoader.loadBitmap(picturePath, holder.mIcon);
			}else
			{
				holder.mIcon.setImageResource(R.drawable.ower_image_none);
			}
			
			if(mCustomType == CustomType.CustomUninvite)
			{
				holder.nButton.setText("邀请");
				holder.nButton.setTextColor(mContext.getResources().getColor(R.color.blue_title_color));
				holder.nButton.setOnClickListener(new OnIntiveListener());
				holder.nButton.setTag(cinfo.getTianYi());
			}
			else
			{
				holder.nButton.setText(cinfo.getMessageCount()+"条未读");
				holder.nButton.setTextColor(mContext.getResources().getColor(R.color.red));
				holder.nButton.setOnClickListener(new OnMessageListener());
				holder.nButton.setTag(cinfo);
			}
		}
		
		
		return convertView;
	}
	
	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		ParentHolder parentHolder;
		if(convertView == null) {
			convertView = mInflater.inflate(R.layout.manage_personal_group_custom, null);
			
			parentHolder = new ParentHolder();
			
			parentHolder.mParentTitle = (TextView) convertView.findViewById(R.id.manage_group_invite_title);
			parentHolder.mParentCount = (TextView) convertView.findViewById(R.id.manage_group_invite_count);
			
			convertView.setTag(parentHolder);
		}else
		{
			parentHolder = (ParentHolder) convertView.getTag();
		}
		
		if(mCustomType == CustomType.CustomUninvite)
		{
			parentHolder.mParentTitle.setText("未加入应用助理的客户");
			int size = mList2.size() - 1;
			if(size < 0)
				size = 0;
			parentHolder.mParentCount.setText(size+"");
		}
		else
		{
			if(mCustomType == CustomType.CustomInvite)
				parentHolder.mParentTitle.setText("我的客户数量");
			else
				parentHolder.mParentTitle.setText("已加入应用助理的客户");
			
			parentHolder.mParentCount.setText(mList1.size()+"");
		}
		
		return convertView;
	}
	
	class ViewHolder {
		ImageView mIcon;
		TextView nName;
		TextView nPhone;
		TextView nBaseLine;
		TextView nMode;
		TextView nExBaseLine;
		TextView nExMode;
		Button nButton;
		
		TextView nMessageTime;
		TextView nMessageCount;
	}

	class ParentHolder {
		TextView mParentTitle;
		TextView mParentCount;
	}
}
